﻿using System;
using CoreEx.Database.Metadata;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;

namespace CoreEx.Database.MetaData.Implementation
{
    [Implements(typeof(ITypeInfoMapper))]
    public class TypeInfoMapper : ITypeInfoMapper, IInitialize
    {
        private ITypeMappingRepository _repository;
        
        public TypeInfoMapper(string connectionName)
        {
            ConnectionName = connectionName;
        }

        public string ConnectionName { get; private set; }
        
        public ITypeMappingInfo MapFrom(Type type)
        {
            return _repository[type];
        }

        public void Initialize(IServiceContainer source)
        {
            _repository = source.GetService<ITypeMappingRepository>(ConnectionName);
        }
    }
}
